
//
// Created by madison on 2022/8/7.
//


#include "vector"
#include "string"
#include "unordered_set"

using namespace std;

class Solution {
public:
    // 方法一：动态规划
    bool wordBreak(string s, vector<string> &wordDict) {
        auto wordDictSet = unordered_set<string>();
        for (auto word: wordDict) {
            wordDictSet.insert(word);
        }
        auto dp = vector<bool>(s.size() + 1);
        dp[0] = true;
        for (int i = 1; i <= s.size(); ++i) {
            for (int j = 0; j < i; ++j) {
                if (dp[j] && wordDictSet.find(s.substr(j, i - j)) != wordDictSet.end()) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.size()];
    }
};

int main() {
    Solution solution;
    vector<string> wordDict = {"apple", "pen"};
    printf("%d", solution.wordBreak("applepenapple", wordDict));
    return 0;
}